﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Circle.Web.Application.Mvc;
using Circle.Web.Drivers.Contract;
using Circle.Web.ViewModelsClient;
using Circle.Domain.Entity;
using Circle.Web.Application;
using Circle.Domain.Interface.Service;
using Circle.Domain.Model;
using Circle.Data;

namespace Circle.Web.Controllers
{
    public class TopicController : BaseSiteMvcController
    {
     
        private ITopicDriver _topicDriver;
        private ICommentDriver _commentDriver;

        public TopicController(ITopicDriver topicDriver, ICommentDriver commentDriver)
        {
            _topicDriver = topicDriver;
            _commentDriver = commentDriver;
        }

        [HttpGet]
        public ActionResult Thread(string key, int? pageIndex)
        {
  
            CircleDbContext context = new CircleDbContext();



            PageTopicFilterClient topicFilter = new PageTopicFilterClient();
            topicFilter.PageIndex = pageIndex.HasValue && pageIndex.Value > 0 ? pageIndex.Value : 0;
            topicFilter.Sort = " CreateTime DESC";
            PageListClient<TopicClientModel> page = _topicDriver.PagingClientTopics(DefaultSetting.TopicsPerPage, topicFilter);
            PageTopicClient pageTopic = new PageTopicClient() { Page = page, Filter = topicFilter };


            pageTopic.Page.GenerateUniqueId();
            return View("Thread", pageTopic);
        }

        [HttpGet]
        public ActionResult Show(Guid key)
        {
            var commentsPerTopic = _topicDriver.GetTopicWithComments(key, 0, DefaultSetting.CommentsPerPage);
            if (commentsPerTopic == null) { return HttpNotFound(); }

            ScriptRegister.AddJson("commentsPerTopic", _topicDriver.ToTopicWithCommentsClient(commentsPerTopic));
            
            var newComment = _commentDriver.Create();
            newComment.TopicId = commentsPerTopic.Topic.Id;
            newComment.Topic = commentsPerTopic.Topic;
            ScriptRegister.AddJson("newComment", _commentDriver.ToClient(newComment));

            return View();
        }

        [HttpGet]
        [AnnaMvcAuthorize]
        public ActionResult New(string key)
        {
            CircleDbContext context = new CircleDbContext();

            var topic = _topicDriver.Create();

            var topicClient = _topicDriver.ToClient(topic);
            ScriptRegister.AddJson("topic", topicClient);

            return View("Record", topicClient);
        }

        [HttpGet]
        [AnnaMvcAuthorize]
        public ActionResult Edit(Guid key)
        {
            var topic = _topicDriver.Get(key);
            if (topic != null && !topic.IsNew())
            {
                TopicClientModel topicClient = _topicDriver.ToClient(topic);
                ScriptRegister.AddJson("topic", topicClient);
                return View("Record", topicClient);
            }
            return HttpNotFound();
        }


     
        public ContentResult Test()
        {
            return Content("test");
        }
    }
}
